package com.lxmes.sales.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;

import com.lxmes.sales.mapper.NcEarlyChukuMapper;
import com.lxmes.sales.domain.NcEarlyChuku;
import com.lxmes.sales.service.INcEarlyChukuService;

/**
 * 前期出库情况管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-14
 */
@Service
public class NcEarlyChukuServiceImpl implements INcEarlyChukuService
{
    @Autowired
    private NcEarlyChukuMapper ncEarlyChukuMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询前期出库情况管理
     *
     * @param id 前期出库情况管理主键
     * @return 前期出库情况管理
     */
    @Override
    public NcEarlyChuku selectNcEarlyChukuById(Long id)
    {
        return ncEarlyChukuMapper.selectNcEarlyChukuById(id);
    }

    /**
     * 查询前期出库情况管理列表
     *
     * @param ncEarlyChuku 前期出库情况管理
     * @return 前期出库情况管理
     */
    @Override
    public List<NcEarlyChuku> selectNcEarlyChukuList(NcEarlyChuku ncEarlyChuku)
    {
        return ncEarlyChukuMapper.selectNcEarlyChukuList(ncEarlyChuku);
    }

    /**
     * 新增前期出库情况管理
     *
     * @param ncEarlyChuku 前期出库情况管理
     * @return 结果
     */
    @Override
    public int insertNcEarlyChuku(NcEarlyChuku ncEarlyChuku)
    {
        return ncEarlyChukuMapper.insertNcEarlyChuku(ncEarlyChuku);
    }

    /**
     * 修改前期出库情况管理
     *
     * @param ncEarlyChuku 前期出库情况管理
     * @return 结果
     */
    @Override
    public int updateNcEarlyChuku(NcEarlyChuku ncEarlyChuku)
    {
        return ncEarlyChukuMapper.updateNcEarlyChuku(ncEarlyChuku);
    }

    /**
     * 批量删除前期出库情况管理
     *
     * @param ids 需要删除的前期出库情况管理主键
     * @return 结果
     */
    @Override
    public int deleteNcEarlyChukuByIds(Long[] ids)
    {
        return ncEarlyChukuMapper.deleteNcEarlyChukuByIds(ids);
    }

    /**
     * 删除前期出库情况管理信息
     *
     * @param id 前期出库情况管理主键
     * @return 结果
     */
    @Override
    public int deleteNcEarlyChukuById(Long id)
    {
        return ncEarlyChukuMapper.deleteNcEarlyChukuById(id);
    }
    @Override
    public String imporIteminfo(List<NcEarlyChuku> ncEarlyChukuList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncEarlyChukuList) || ncEarlyChukuList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcEarlyChuku ncEarlyChuku : ncEarlyChukuList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncEarlyChuku);
                ncEarlyChuku.setCreateBy(operName);
                ncEarlyChuku.setUpdateBy(operName);
                this.insertNcEarlyChuku(ncEarlyChuku);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
